using System;
using System.Collections.Generic;
using System.Text;
using emp.IDAL.SysManage;
using emp.Model.SysManage;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using emp.Utility;

namespace emp.SqlServerDAL.SysManage
{
    public class UserOrganiz : IUserOrganiz
    {

        #region IUserOrganiz Members

        private const string SQL_SELECT = " select user_organiz_uid,user_uid,organization_uid from user_organization where user_uid = @User_ID ";
        private const string SQL_INSERT = " exec p_save_userorganiz @User_ID,@Organiz_IDStr ";

        private const string PARM_USORGANIZ_ID = "@UserOrganiz_ID";
        private const string PARM_USERID = "@User_ID";
        private const string PARM_ORGANIZ_ID = "@Organiz_ID";
        private const string PARM_ORGANIZ_IDSTR = "@Organiz_IDStr";

        public int Insert(UserOrganizInfo myUserOrganiz)
        {

            string sOrganizIDStr = myUserOrganiz.OrganizIDStr.Trim();

            string[] organizArray = sOrganizIDStr.Split(new char[] { ',' });

            string sSqlInsert = " delete from user_organization where user_uid = " + myUserOrganiz.UserID.ToString().Trim() + ";";

            foreach(string sOrganizID in organizArray)
            {
                sSqlInsert += " insert into user_organization values(" + myUserOrganiz.UserID.ToString().Trim() + "," + sOrganizID + ");";
            }


            int returnValue = 0;
            using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        returnValue = SQLHelper.ExecuteNonQuery(trans, CommandType.Text, sSqlInsert, null);
                        trans.Commit();
                    }
                    catch (SqlException err)
                    {
                        trans.Rollback();
                        string sMethod = "emp.SqlServerDAL.User.Insert()";
                        EventsLog.WriteLog(err, sMethod);
                    }
                }
            }
            return returnValue;
        }

        public IList getUserOrganizList(int iUserID)
        {
            IList userList = new ArrayList();

            SqlParameter parm = new SqlParameter(PARM_USERID, SqlDbType.Int, 4, "user_uid");
            parm.Value = iUserID;

            //Execute a query to read the products
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT, parm))
            {
                while (rdr.Read())
                {
                    UserOrganizInfo myUserOrganizInfo = new UserOrganizInfo();
                    myUserOrganizInfo.UserOrganizID = rdr.GetInt32(0);
                    myUserOrganizInfo.UserID = rdr.GetInt32(1);
                    myUserOrganizInfo.OrganizID = rdr.GetInt32(2);

                    userList.Add(myUserOrganizInfo);
                }
            }

            return userList;
        }

        #endregion
    }
}
